/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2021. All rights reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.daasuu.countanimationtextview.slice;

import com.daasuu.cat.CountAnimationTextView;
import com.daasuu.countanimationtextview.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;

import java.text.DecimalFormat;

/**
 * Main ability slice
 */
public class MainAbilitySlice extends AbilitySlice {
    /**
     * The constant M count animation text view
     */
    private CountAnimationTextView mCountAnimationTextView;

    /**
     * On start *
     *
     * @param intent intent
     */
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        mCountAnimationTextView = (CountAnimationTextView) findComponentById(ResourceTable.Id_count_animation_textView);

        Button button0 = (Button) findComponentById(ResourceTable.Id_btn_count_anim_0);
        button0.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                mCountAnimationTextView
                        .setAnimationDuration(5000)
                        .countAnimation(0, 99999);
            }
        });

        Button button1 = (Button) findComponentById(ResourceTable.Id_btn_count_anim_1);
        button1.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                mCountAnimationTextView
                        .setAnimationDuration(5000)
                        .countAnimation(99999, 0);
            }
        });

        Button button2 = (Button) findComponentById(ResourceTable.Id_btn_count_anim_2);
        button2.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                mCountAnimationTextView
                        .setDecimalFormat(new DecimalFormat("###,###,###"))
                        .setAnimationDuration(10000)
                        .countAnimation(0, 9999999);
            }
        });

        mCountAnimationTextView.setCountAnimationListener(new CountAnimationTextView.CountAnimationListener() {
            @Override
            public void onAnimationStart(Object animatedValue) {
                // do nothing
            }

            @Override
            public void onAnimationEnd(Object animatedValue) {
                mCountAnimationTextView.clearDecimalFormat();
            }
        });
    }

    /**
     * On active
     */
    @Override
    public void onActive() {
        super.onActive();
    }

    /**
     * On foreground *
     *
     * @param intent intent
     */
    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
